/**
 *
 */
package com.rf.richfitwheel.admin.sys.service.impl;

import com.rf.richfitwheel.admin.sys.dao.UserMngOrgMapper;
import com.rf.richfitwheel.admin.sys.model.UserMngOrg;
import com.rf.richfitwheel.admin.sys.model.UserMngOrgExample;
import com.rf.richfitwheel.admin.sys.service.SysUserMngOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhanglu
 *
 */
@Service("sysUserMngOrgService")
public class SysUserMngOrgServiceImpl implements SysUserMngOrgService {

    @Autowired
    private UserMngOrgMapper userMngOrgMapper;

    @Override
    @Cacheable(value = "mngOrg", key = "'getDefaultMngOrgByUserId:' + #userId") //缓存
    public UserMngOrg getDefaultMngOrgByUserId(String userId) {
        UserMngOrgExample example = new UserMngOrgExample();
        UserMngOrgExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserMngOrg> list = userMngOrgMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    @Cacheable(value = "mngOrg", key = "'getListByUserId:' + #userId") //缓存
    public List<UserMngOrg> getListByUserId(String userId) {
        UserMngOrgExample example = new UserMngOrgExample();
        UserMngOrgExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserMngOrg> list = userMngOrgMapper.selectByExample(example);
        return list;
    }

    @Override
    @CacheEvict(value = "mngOrg", allEntries = true)
    public void deleteByUserId(String userId) {
        List<UserMngOrg> userMngOrgs = this.getListByUserId(userId);
        for (UserMngOrg userMngOrg : userMngOrgs) {
            userMngOrgMapper.deleteByPrimaryKey(userMngOrg.getId());
        }
    }
    
    /**
     * 根据机构id查询管辖信息
     *
     * @param orgId 机构id
     * @return 管辖信息
     */
    @Override
    @Cacheable(value = "mngOrg", key = "'getListByOrgId:' + #orgId") //缓存
    public List<UserMngOrg> getListByOrgId(String orgId){
        UserMngOrgExample example = new UserMngOrgExample();
        UserMngOrgExample.Criteria criteria = example.createCriteria();
        criteria.andOrgIdEqualTo(orgId);
        return userMngOrgMapper.selectByExample(example);
    }
    
    /**
     * 查询管辖某个机构的人员
     *
     * @param orgId 机构id
     * @return 用户列表
     */
    @Override
    @Cacheable(value = "mngOrg", key = "'getMngUsersListByOrgId:' + #orgId") //缓存
    public List<Map<String, Object>> getMngUsersListByOrgId(String orgId){
        return userMngOrgMapper.getMngUsersListByOrgId(orgId);
    }
}
